package cn.xiuxian.dao.impl;

import cn.xiuxian.dao.IUserDao;
import cn.xiuxian.entity.User;

import java.sql.*;
import java.util.Date;
import java.util.List;

@SuppressWarnings("all")
public class UserDaoImpl implements IUserDao {
    @Override
    public int save(User user) {
        Connection connection = null;
        Statement statement = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            String  url = "jdbc:mysql://localhost:3306/dy?useSSL=false&useUnicode=true&characterEncoding=utf8&serverTimezone=UTC";
            String  username = "root";
            String  password = "root";
            connection = DriverManager.getConnection(url,username,password);
            statement = connection.createStatement();

            //  insert into jdbc_user(username,password,birthday,power) values(3,'蔡根花','123','2021-01-02',1)
            //DATE转字符串
            String bt = String.format("%tF",user.getBirthday());
            String sql = "insert into  jdbc_user(username,password,birthday,power) values ('"+user.getUsername()+"','"+user.getPassword()+"','"+bt+"',"+user.getPower()+")";
           boolean result = statement.execute(sql);
            if (result) { return  1;}

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            if(statement != null){
                try {
                    statement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(connection != null){
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return 0;
    }

    @Override
    public User findByUsername(String username) {
        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;
        User user = null;
        try {
            //1.
            Class.forName("com.mysql.cj.jdbc.Driver");
            //2.
            String  url = "jdbc:mysql://localhost:3306/dy?useSSL=false&useUnicode=true&characterEncoding=utf8&serverTimezone=UTC";
            String  userName = "root";
            String  password = "root";
            connection = DriverManager.getConnection(url,userName,password);
            //3.
            statement = connection.createStatement();
            //4.
            String sql = "select *  from  jdbc_user  where  username = '" + username +"'";
            resultSet = statement.executeQuery(sql);

            //5.
            if (resultSet.next()){
                Integer id = resultSet.getInt(1);
                String  uname = resultSet.getString(2);
                String   pwd = resultSet.getString(3);
                Date    birthday = resultSet.getDate(4);
                Integer  power = resultSet.getInt(5);
                user = new User(id,uname,pwd,birthday,power);
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            if (resultSet != null){
                try {
                    resultSet.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (statement != null){
                try {
                    statement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(connection != null){
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }

        return  user;
    }

    @Override
    public void saveList(List<User> userList) {

    }

    @Override
    public void saveBatch(List<User> userList) {

    }

    @Override
    public int deleByid(Integer id) {
        return 0;
    }

    @Override
    public Object getUserByid(Integer id) {
        return null;
    }

    @Override
    public int deleteByid(User user, Integer id) {
        return 0;
    }

    @Override
    public User getUserByid(User user, Integer id) {
        return null;
    }

    @Override
    public List<User> getAllUser(User user) {
        return null;
    }

    @Override
    public List<User> getAll(User user) {
        return null;
    }
}
